﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Practices.ObjectBuilder2;
using TciNewsReader_2014_04.Infrastructure;
using TciNewsReader_2014_04.Model;
using TciNewsReader_2014_04.VmHelpers;
namespace TciNewsReader_2014_04
{
    public partial class ViewModel
    {
        public ICommand ShowConfigCommand
        {
            get { return new RelayCommand(ExecuteShowConfigCommand, CanExecuteShowConfigCommand);}
        }
        public ICommand ShowFeedCrudCommand
        {
            get { return new RelayCommand(ExecuteShowFeedCrudCommand, CanExecuteShowFeedCrudCommand); }
        }
        public ICommand FeedCrudDoneCommand
        {
            get { return new RelayCommand(ExecuteFeedCrudDoneCommand, CanExecuteFeedCrudDoneCommand); }
        }
        public ICommand FeedCrudVerifyAllCommand
        {
            get { return new RelayCommand(ExecuteFeedCrudVerifyAllCommand, CanExecuteFeedCrudVerifyAllCommand); }
        }
        public ICommand ShowErrorsCommand
        {
            get { return new RelayCommand(ExecuteShowErrorsCommand, CanExecuteShowErrorsCommand); }
        }
        public ICommand OpenArticleCommand
        {
            get { return new RelayCommand(ExecuteOpenArticleCommand, CanExecuteOpenArticleCommand); }
        }
        public ICommand GoToNextItemCommand
        {
            get { return new RelayCommand(ExecuteGoToNextItemCommand, CanExecuteGoToNextItemCommand); }
        }
        public ICommand GoToPreviousItemCommand
        {
            get { return new RelayCommand(ExecuteGoToPreviousItemCommand, CanExecuteGoToPreviousItemCommand); }
        }
        public ICommand RefreshCommand
        {
            get { return new RelayCommand(ExecuteRefreshCommand, CanExecuteRefreshCommand); }
        }
        public ICommand OpenIncludeExcludeCommand
        {
            get { return new RelayCommand(ExecuteOpenIncludeExcludeCommand, CanExecuteOpenIncludeExcludeCommand); }
        }
        public ICommand IncludeExcludeCheckAllCommand
        {
            get { return new RelayCommand(ExecuteIncludeExcludeCheckAllCommand, CanExecuteIncludeExcludeCheckAllCommand); }
        }
        public ICommand IncludeExcludeDoneCommand
        {
            get { return new RelayCommand(ExecuteIncludeExcludeDoneCommand, CanExecuteIncludeExcludeDoneCommand); }
        }
        public ICommand HelpAboutCommand
        {
            get { return new RelayCommand(ExecuteHelpAboutCommand, CanExecuteHelpAboutCommand); }
        }
        public ICommand HelpAboutDoneCommand
        {
            get { return new RelayCommand(o => _helpAboutInitializer.Close(), o => true); }
        }
        public ICommand ErrorReportDoneCommand
        {
            get { return new RelayCommand(o => _errorReportInitializer.Close(), o => true); }
        }
        private void ExecuteHelpAboutCommand(object obj)
        {
            _helpAboutInitializer.Initialize();
        }
        private bool CanExecuteHelpAboutCommand(object obj)
        {
            return true;
        }
        private void ExecuteIncludeExcludeDoneCommand(object obj)
        {
            if (obj != null)
            {
                bool trueFalse;
                if (bool.TryParse(obj.ToString(), out trueFalse))
                {
                    if (trueFalse)
                    {
                        ItemWrappers.ForEach(FilteringHandler);
                        FeedInfos.ForEach(q => q.RaiseNotifyOnCounts());
                    }
                    else
                    {
                        FeedInfos.ForEach(q => q.PopOldValuesIncludeInView());
                    }
                }
                _includeExcludeInitializer.Close();
                _itemWrappersListCollectionView.Refresh();
                UpdateCountsStatus();
            }
        }
        private bool CanExecuteIncludeExcludeDoneCommand(object obj)
        {
            if (obj != null)
            {
                string cmd = obj.ToString();
                if (cmd == "true")
                {
                    if (!FeedInfos.Any(q => q.IncludeInView)) return false;
                }
            }
            return true;
        }
        private void ExecuteIncludeExcludeCheckAllCommand(object obj)
        {
            if (obj != null)
            {
                bool trueFalse;
                if (bool.TryParse(obj.ToString(), out trueFalse))
                {
                    FeedInfos.ForEach(q => q.IncludeInView = trueFalse);
                }
                UpdateCountsStatus();
            }
        }
        private bool CanExecuteIncludeExcludeCheckAllCommand(object obj)
        {
            if (obj != null)
            {
                bool trueFalse;
                if (bool.TryParse(obj.ToString(), out trueFalse))
                {
                    if (trueFalse)
                    {
                        if (FeedInfos.Any(q => !q.IncludeInView))
                        {
                            return true;
                        }
                        return false;
                    }
                    if (FeedInfos.Any(q => q.IncludeInView))
                    {
                        return true;
                    }
                    return false;
                }
            }
            return true;
        }
        private void ExecuteOpenIncludeExcludeCommand(object obj)
        {
            foreach (FeedInfo fi in FeedInfos)
            {
                // save current in case of cancel
                fi.PushIncludeInView();
            }
            _includeExcludeInitializer.Initialize();
        }
        private bool CanExecuteOpenIncludeExcludeCommand(object obj)
        {
            return true;
        }
        private void ExecuteRefreshCommand(object obj)
        {
            RefreshAllFeeds();
        }
        private bool CanExecuteRefreshCommand(object obj)
        {
            if (IsBusy) return false;
            if (!IsReadyForInteraction) return false;
            if (FeedInfos.Any(q => !q.WrappersHaveBeenProcessed)) return false;
            return true;
        }
        private void ExecuteGoToPreviousItemCommand(object obj)
        {
            _itemWrappersListCollectionView.MoveCurrentToPrevious();
        }
        private bool CanExecuteGoToPreviousItemCommand(object obj)
        {
            if (!IsReadyForInteraction) return false;
            if (_itemWrappersListCollectionView.CurrentPosition - 1 >= 0)
            {
                return true;
            }
            return false;
        }
        private void ExecuteGoToNextItemCommand(object obj)
        {
            _itemWrappersListCollectionView.MoveCurrentToNext();
        }
        private bool CanExecuteGoToNextItemCommand(object obj)
        {
            if (!IsReadyForInteraction) return false;
            if (_itemWrappersListCollectionView != null && _itemWrappersListCollectionView.CurrentPosition + 1 < _itemWrappersListCollectionView.Count)
            {
                return true;
            }
            return false;
        }
        private void ExecuteOpenArticleCommand(object obj)
        {
            Process.Start(MasterViewSelectedItem.Link);
        }
        private bool CanExecuteOpenArticleCommand(object obj)
        {
            if (!IsReadyForInteraction) return false;
            if (MasterViewSelectedItem == null) return false;
            if (String.IsNullOrEmpty(MasterViewSelectedItem.Link)) return false;
            return true;
        }
        private void ExecuteShowErrorsCommand(object obj)
        {
            if (ExceptionInfos.Count == 0)
            {
                _confirmations.Inform("Nothing to show.");
                return;
            }
            _errorReportInitializer.Initialize();
        }
        private bool CanExecuteShowErrorsCommand(object obj)
        {
            return true;
        }
        private void ExecuteShowFeedCrudCommand(object obj)
        {
            _persistenceManager.GetUrlList(l =>
            {
                PopulateFeedCruds(l);
                _crudNotifyCollectionChangedEventArgs.Clear();
                _feedCrudInitializer.Initialize();
            });
        }
        private bool CanExecuteShowFeedCrudCommand(object obj)
        {
            return true;
        }
        private void ExecuteFeedCrudDoneCommand(object obj)
        {
            bool refreshNeeded = false;
            if (obj != null)
            {
                string cmd = obj.ToString();
                if (cmd == "true")
                {
                    var itemsToSave = new List<string>();
                    CrudWrappers.ForEach(q => itemsToSave.Add(q.Url));
                    _persistenceManager.SaveUrlList(itemsToSave);
                    refreshNeeded = true;
                }
            }
            _feedCrudInitializer.Close();
            if (refreshNeeded && CanExecuteRefreshCommand(null))
            {
                FeedInfos.Clear();
                ExecuteRefreshCommand(null);
            }
        }
        private bool CanExecuteFeedCrudDoneCommand(object obj)
        {
            if (obj != null)
            {
                string cmd = obj.ToString();
                if (cmd == "false")
                {
                    return true;
                }
                if (CrudWrappers.Count == 0) return false;
                if (!CrudWrappers.All(q => q.CanUseThisUrl())) return false;
                if (CrudWrappers.All(q => q.State == CrudState.Unchanged))
                {
                    if (_crudNotifyCollectionChangedEventArgs.Any(q => q.Action == NotifyCollectionChangedAction.Remove))
                    {
                        return true;
                    }
                }
                if (CrudWrappers.Any(q => q.State == CrudState.Created)) return true;
                return false;
            }
            return false;
        }
        private void ExecuteFeedCrudVerifyAllCommand(object obj)
        {
            CrudWrappers.ForEach(q => q.Verify());
            OnPropertyChanged("FeedCrudDoneCommand");
        }
        private bool CanExecuteFeedCrudVerifyAllCommand(object obj)
        {
            return CrudWrappers.All(q => q.PassedValidation);
        }
        private void ExecuteShowConfigCommand(object obj)
        {
            _configInitializer.Initialize();
        }
        private bool CanExecuteShowConfigCommand(object obj)
        {
            if (!IsReadyForInteraction) return false;
            return true;
        }
        public ICommand ConfigDoneCommand
        {
            get { return new RelayCommand(o => _configInitializer.Close(), o => true); }
        }
        private void NotifyCommandChanges()
        {
            OnPropertyChanged("RefreshCommand");
            OnPropertyChanged("GoToPreviousItemCommand");
            OnPropertyChanged("GoToNextItemCommand");
            OnPropertyChanged("OpenArticleCommand");
            OnPropertyChanged("ShowConfigCommand");
        }
    }
}